package com.xhwl.data.controller;


import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.pojo.cent.sdata.Account;
import com.xhwl.common.pojo.cent.sdata.SdataAccountQuickDataBoard;
import com.xhwl.common.utils.ChkUtil;
import com.xhwl.data.pojo.AccountBoardRoleDTO;
import com.xhwl.data.pojo.dto.AccountBoardDTO;
import com.xhwl.data.service.IPermissionDataBoardService;
import com.xhwl.data.util.AccountUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 数据看板权限设置
 * updated_at: 2022-10-24 主数据升级，角色（项目级）变更为企业级
 */
@RestController
public class PermissionDataBoardController {

    @Autowired
    IPermissionDataBoardService iPermissionDataBoardService;

    /**
     * 数据看板根据角色查询数据看板URL
     */
    @GetMapping(value = "/data-board/detail")
    public ResultJson<SdataAccountQuickDataBoard> detailDataBoard(Account operateAccount,
                                                                  @RequestParam(value = "roleId") Integer roleId,
                                                                  @RequestParam(value = "projectId", required = false) Integer projectId) {
        AccountUtil.checkLogin(operateAccount);
        return ResultJson.success(iPermissionDataBoardService.detail(roleId, projectId));
    }

    /**
     * 保存数据看板
     */
    @PostMapping(value = "/data-board/save")
    public ResultJson saveQuickEntry(Account operateAccount, @Validated @RequestBody AccountBoardDTO accountBoardDTO) {
        if (accountBoardDTO.getType() == 2 && ChkUtil.isNull(accountBoardDTO.getDataUrl())) {
            return ResultJson.fail("类型为数据请配置看板URL");
        }
        AccountUtil.checkLogin(operateAccount);
        iPermissionDataBoardService.save(accountBoardDTO);
        return ResultJson.success();
    }

    /**
     * 查询数据看板第一次进入的角色
     */
    @GetMapping(value = "/data-board/role/query")
    public ResultJson<AccountBoardRoleDTO> queryDataRole(Account operateAccount, @RequestParam(value = "projectId", required = false) Integer projectId) {
        AccountUtil.checkLogin(operateAccount);
        AccountBoardRoleDTO accountBoardRoleDTO = new AccountBoardRoleDTO();
        if (ChkUtil.isNotNull(projectId)) {
            accountBoardRoleDTO.setProjectId(projectId);
        }
        accountBoardRoleDTO.setUserId(operateAccount.getId());
        AccountBoardRoleDTO boardRoleDTO = iPermissionDataBoardService.queryDataRole(accountBoardRoleDTO);
        return ResultJson.success(boardRoleDTO == null ? new AccountBoardRoleDTO() : boardRoleDTO);
    }

    /**
     * 保存数据看板第一次进入的角色
     */
    @PostMapping(value = "/data-board/role/save")
    public ResultJson saveDataRole(Account operateAccount, @Validated @RequestBody AccountBoardRoleDTO accountBoardRoleDTO) {
        AccountUtil.checkLogin(operateAccount);
        accountBoardRoleDTO.setUserId(operateAccount.getId());
        iPermissionDataBoardService.saveDataRole(accountBoardRoleDTO);
        return ResultJson.success();
    }
}


